﻿Imports System.Data.OleDb
Public Class frmDonViTinh
#Region "Form Event"
    Private RowIndex As Integer = 0
    Private DonViId As Integer = -1
    Private Sub frmDonViTinh_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            lblEmpty.Visible = False
            txtTenDonVi.Text = ""
            DonViTinh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtTenDonVi.Text = "" Then
                lblEmpty.Visible = True
                EmptyData(txtTenDonVi)
                Exit Sub
            Else
                lblEmpty.Visible = False
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If DonViId = -1 Then
                query = "Insert Into DonViTinh(TenDonVi) Values(N'" & txtTenDonVi.Text & "')"
            Else
                query = "Update DonViTinh Set TenDonVi=N'" & txtTenDonVi.Text & "' Where IdDonVi=" & DonViId
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtTenDonVi.Text = ""
            DonViId = -1
            DonViTinh_Load()
            'frmThemQu.DonViTinh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtTenDonVi.Text = ""
            DonViId = -1
            'frmThemQuyCach.DonViTinh_Load()
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            DonViId = dgvDonViTinh.Rows(RowIndex).Cells("IdDonVi").Value
            txtTenDonVi.Text = dgvDonViTinh.Rows(RowIndex).Cells("TenDonVi").Value
            RowIndex = 0
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            txtTenDonVi.Text = ""
            DonViId = -1
            Dim IdDonVi As Integer = dgvDonViTinh.Rows(RowIndex).Cells("IdDonVi").Value
            Dim TenDonVi As String = dgvDonViTinh.Rows(RowIndex).Cells("TenDonVi").Value
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa  : " & TenDonVi, _
                  "Xóa Đơn Vị Tính", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Update DonViTinh Set Xoa=1 Where IdDonVi=" & IdDonVi
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                DonViTinh_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub dgvDonViTinh_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDonViTinh.CellClick
        Try
            RowIndex = e.RowIndex
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"
    Private Sub DonViTinh_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsTinhThanh As New DataSet
            Dim dtTinhThanh As New DataTable
            Dim query As String = "Select * From DonViTinh Where Xoa=0 Order By TenDonVi ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsTinhThanh)
            dbConn.Close()
            dtTinhThanh = dsTinhThanh.Tables(0)
            dtTinhThanh.Columns.Add("STT")
            For i As Integer = 0 To dtTinhThanh.Rows.Count - 1
                dtTinhThanh.Rows(i)("STT") = i + 1
            Next
            dgvDonViTinh.AutoGenerateColumns = False
            dgvDonViTinh.DataSource = dtTinhThanh
            If dgvDonViTinh.Rows.Count - 1 > 0 Then
                dgvDonViTinh.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region

    Private Sub txtTenDonVi_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenDonVi.KeyUp
        Try
            txtTenDonVi.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub
End Class